package Server.ExtComponents.socket;

import Server.ExtComponents.BaseConfig.MServerConfig;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.traffic.ChannelTrafficShapingHandler;
import org.jboss.netty.handler.traffic.GlobalTrafficShapingHandler;
import org.jboss.netty.handler.traffic.TrafficCounter;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

import java.util.NoSuchElementException;


public class MServerFrameUtil{


    public static final Timer timer = new HashedWheelTimer(); //计时器（底层读写数据是否超时）,timer must be shared
    /**游戏系统流量监控**/
    public static final GlobalTrafficShapingHandler globalTrafficShapingHandler=new GlobalTrafficShapingHandler(timer, MServerConfig.checkInterval);

    /**
     * 返回ChannelTrafficShapingHandler 对象  per to per
     * @return
     */
    public  static ChannelTrafficShapingHandler getChannelTrafficShapingHandler(){
        return  new ChannelTrafficShapingHandler(timer, MServerConfig.checkInterval);
    }


    /**
     * all
     *启动流量监控器
     */
    public static void startMonitor(){
        globalTrafficShapingHandler.getTrafficCounter().start();
    }

    /**
     * all
     * 停止流量监控器
     */
    public static void stopMonitor(){
        globalTrafficShapingHandler.getTrafficCounter().stop();
    }


    /**
     * all
     * 重置流量统计周期
     * @param newcheckInterval
     */
    public static void configure(long newcheckInterval){

        globalTrafficShapingHandler.getTrafficCounter().configure(newcheckInterval);
    }


    /**
     *  all
     *  返回在interval周期中读取流量的统计
     *  @return the Read Throughput in KB/s computes in the last check interval
     */
    public static String ReadThroughput(){
       return  (globalTrafficShapingHandler.getTrafficCounter().getLastReadThroughput()<<10)+"KB/s";
    }


    /**
     *  all
     *  返回在interval周期中读取流量的统计
     *  @return the Read Throughput in KB/s computes in the last check interval
     */
    public static String WriteThroughput(){
        return  (globalTrafficShapingHandler.getTrafficCounter().getLastWriteThroughput()<<10)+"KB/s";
    }



    /**
     * 单个连接 流量统计
     * @param trafficCounter
     * @return KB/s
     */
    public static String ReadThroughputPer(TrafficCounter trafficCounter){
        return (trafficCounter.getLastReadThroughput()<<10)+"KB/s";
    }

    /**
     *单个连接 流量统计
     * @param trafficCounter
     * @return  KB/s
     */
    public static String WriteThroughputPer(TrafficCounter trafficCounter){
        return (trafficCounter.getLastWriteThroughput()<<10)+"KB/s";
    }





    /**
     * 清理  ChannelPipeline资源
     * @param pipeline  pipeline
     */
    public static final void clearPipeline(ChannelPipeline pipeline){
        if(null == pipeline){
            return;
        }
        try{
            while(pipeline.getLast()!= null){
                pipeline.removeLast();
            }
            pipeline=null;
        }catch (NoSuchElementException e){
            // all elements removed.
        }
    }


    public static void main(String[]args){


        System.out.println(10000>>10);

    }
}
